package com.atguigui.leetcode;

import java.util.Arrays;

/**
 * 1710.卡车上的最大单元数
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/11/15 8:52
 */
public class P1710MaximumUnitsOnATruck {
    public static void main(String[] args) {
        Solution solution = new P1710MaximumUnitsOnATruck().new Solution();
        // TO TEST
    }

    class Solution {
        public int maximumUnits(int[][] boxTypes, int truckSize) {
            //根据单元数量进行排序
            Arrays.sort(boxTypes, (a, b) -> b[1] - a[1]);
            int ans = 0;
            for (int[] boxType : boxTypes) {
                if (boxType[0] > truckSize) {
                    ans += truckSize * boxType[1];
                    break;
                } else {
                    ans += boxType[0] * boxType[1];
                    truckSize -= boxType[0];
                }
            }

            return ans;
        }
    }
}
